//
//  ZJStudyCourseQuestionsTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/6/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJStudyCourseQuestionsTableViewCell.h"
#import "ZJSCQusetionsTableViewCell.h"
#import "ZJSCQusetionsSrectionView.h"
#import "ZJSCUnDataTableViewCell.h"


#define CellID @"ZJSCQusetionsTableViewCell"
#define SectionID @"ZJSCQusetionsSrectionView"
#define UNDATACELL   @"ZJSCUnDataTableViewCell"
@interface ZJStudyCourseQuestionsTableViewCell ()<UITableViewDelegate,UITableViewDataSource,ZJSCQusetionsSrectionViewDelegate,ZJSCQusetionsTableViewCellDelegate>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据源 */
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ZJStudyCourseQuestionsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.tableView];
        
        self.tableView.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .bottomSpaceToView(self.contentView, 0);

    }
    return self;
}

#pragma mark - ZJSCQusetionsTableViewCellDelegate （评论cell）协议方法

//Cell上的按钮被点击
- (void)ZJSCQusetionsTableViewCellDelegate_operateSuccessToModel:(ZJSCQusetionsCellModel *)model indexPatch:(NSIndexPath *)indexPatch{
    ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[indexPatch.section];
    sectionModel.childModelArray[indexPatch.row] = model;
    
    self.dataArray[indexPatch.section] = sectionModel;
}
#pragma mark - ZJSCQusetionsSrectionViewDelegate（评论SectionView） 协议方法
//(点赞或收藏)sectionView上的按钮点击并网络请求完成
- (void)ZJSCQusetionsSrectionViewDelegate_operateSuccessToModel:(ZJSCQusetionsSrectionModel *)model section:(NSInteger)section{
    
    self.dataArray[section] = model;
}

#pragma mark - UITableView 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.dataArray[section] isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
    ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[section];
    return sectionModel.CHILD_CLASS_FAQ.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.dataArray[indexPath.section] isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[indexPath.section];
        ZJSCQusetionsCellModel *cellModel = sectionModel.childModelArray[indexPath.row];
        
        ZJSCQusetionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
        cell.model = cellModel;
        cell.indexPatch = indexPath;
        cell.delegate = self;
        if (indexPath.row == sectionModel.CHILD_CLASS_FAQ.count - 1) {
            cell.isLastCell = YES;
        }else{
            cell.isLastCell = NO;
        }
        return cell;
    }else{
        NSDictionary *dict = self.dataArray[indexPath.section];
        if ([[dict objectForKey:@"type"] isEqualToString:@"1"]) {//点击更多
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = [UIFont titleTextFont];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.textColor = [UIColor textBlackColor];
            cell.textLabel.text = dict[@"name"];
            cell.textLabel.backgroundColor = [UIColor clearColor];
            cell.contentView.backgroundColor = [UIColor viewBGGrayColor];
            return cell;
            
        }else{//显示无数据图片
            ZJSCUnDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UNDATACELL forIndexPath:indexPath];
            cell.unDataImageView.image = [UIImage imageNamed:[dict objectForKey:@"imageName"]];
            return cell;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if ([self.dataArray[section] isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[section];
        ZJSCQusetionsSrectionView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionID];
        view.model = sectionModel;
        view.section = section;
        view.delegate = self;
        view.commentDelegate = self.delegate;
        
        return view;
        
    }else{
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
        return view;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.dataArray[indexPath.section] isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[indexPath.section];
        ZJSCQusetionsCellModel *cellModel = sectionModel.childModelArray[indexPath.row];
        
        if (indexPath.row == sectionModel.CHILD_CLASS_FAQ.count - 1) {
            cellModel.isLastCell = YES;
        }else{
            cellModel.isLastCell = NO;
        }
        
        return cellModel.cellHeight;

    }else{
        
        NSDictionary *dict = self.dataArray[indexPath.section];
        if ([[dict objectForKey:@"type"] isEqualToString:@"1"]) {//点击更多
            return 44 * [UIScreen zj_screenHScale];
        }else{//无数据
            return 88 * [UIScreen zj_screenHScale];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if ([self.dataArray[section] isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[section];
        return sectionModel.sectionHeight;
    }else{
        return 0.0001;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.textLabel.text isEqualToString:@"点击查看更多评论"]) {
        if (self.delegate) {
            [self.delegate ZJStudyCourseQuestionsTableViewCellDelegate_clickMoreComment];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}



#pragma mark - Setter
- (void)setArray:(NSArray *)array{
    _array = array;
    self.dataArray = [array mutableCopy];
    [self.tableView reloadData];
}

#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 100) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
       [_tableView  registerClass:[UITableViewCell class] forCellReuseIdentifier: @"Cell"];
        [_tableView  registerClass:[ZJSCQusetionsTableViewCell class] forCellReuseIdentifier: CellID];
        [_tableView registerClass:[ZJSCQusetionsSrectionView class] forHeaderFooterViewReuseIdentifier: SectionID];
        [_tableView registerClass:[ZJSCUnDataTableViewCell class] forCellReuseIdentifier: UNDATACELL];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}


@end
